<template>
  <div class="app-container">
    <div>
      <div style="margin-top: 15px;margin-bottom: 15px;">
        <el-button type="success" @click="showCreate">添加直播</el-button>
      </div>
      <div>
        <el-table border :data="table.data" v-loading="table.loading">
          <el-table-column label="标题" prop="title"></el-table-column>
          <el-table-column label="主讲人" prop="teacher"></el-table-column>
          <el-table-column label="开始时间" prop="start_time"></el-table-column>
          <el-table-column label="结束时间" prop="end_time"></el-table-column>
          <el-table-column label="是否显示">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.is_show===1" type="success">显示</el-tag>
              <el-tag v-if="scope.row.is_show===2" type="danger">不显示</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="sort"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="showEdit(scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-if="table.total > 0" style="margin-top: 15px;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="table.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="table.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="table.total">
        </el-pagination>
      </div>
    </div>
    <div>
      <el-dialog :visible.sync="createDialog.visible" :title="createDialog.title" @close="handleClose" v-if="createDialog.visible">
        <el-form :model="postForm" :rules="createDialog.rules" ref="postForm">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="主讲人" prop="teacher">
                <el-input v-model="postForm.teacher" placeholder="请输入主讲人"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item label="直播标题" prop="title">
                <el-input v-model="postForm.title" placeholder="请输入直播标题"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="开始时间" prop="start_time">
                <el-date-picker type="datetime" v-model="postForm.start_time" placeholder="请输入直播开始时间" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="结束时间" prop="end_time">
                <el-date-picker type="datetime" v-model="postForm.end_time" placeholder="请输入直播结束时间"  value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="缩略图" prop="thumbnail">
<!--            <el-input v-model="postForm.thumbnail" placeholder="请输入缩略图地址"></el-input>-->
            <el-upload :action="createDialog.action"
                       :headers="createDialog.headers"
                       :data = "createDialog.thumbnailData"
                       :file-list="createDialog.thumbnailList"
                       :limit="1" list-type="picture-card" :on-preview="handleImagePreview" :on-remove="handleImageRemove('thumbnail')" :on-success="handleUploadSuccess('thumbnail')">
              <i slot="default" class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="直播二维码图片" prop="live_imgae_url">
<!--            <el-input v-model="postForm.live_imgae_url" placeholder="请输入直播二维码图片地址"></el-input>-->
            <el-upload :action="createDialog.action"
                       :headers="createDialog.headers"
                       :data = "createDialog.liveData"
                       :file-list="createDialog.liveImageList" list-type="picture-card" :limit="1" :on-preview="handleImagePreview" :on-success="handleUploadSuccess('live_imgae_url')" :on-remove="handleImageRemove('live_imgae_url')">
              <i slot="default" class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="回放视频地址" prop="live_video_url">
            <el-input v-model="postForm.live_video_url" placeholder="请输入回放视频地址"></el-input>
          </el-form-item>
          <el-form-item label="主讲人介绍" prop="teacher_introduction">
            <el-input type="textarea" v-model="postForm.teacher_introduction" placeholder="请输入主讲人介绍" :rows="3"></el-input>
          </el-form-item>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="排序" prop="sort">
                <el-input v-model.number="postForm.sort"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否显示" prop="is_show">
                <el-switch
                  v-model="postForm.is_show"
                  active-color="#13ce66"
                  :active-value="1"
                  active-text="是"
                  inactive-text="否"
                  :inactive-value="2"
                  inactive-color="#ff4949">
                </el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="直播类型" prop="live_type">
                <el-radio-group v-model="postForm.live_type">
                  <el-radio :label="1">每周直播</el-radio>
                  <el-radio :label="2">默认</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item>
            <el-button type="success" @click="handleCreate" :disabled="createDialog.disabled.saveButton"  v-if="createDialog.type==='create'">保存</el-button>
            <el-button type="success" @click="handleEdit" :disabled="createDialog.disabled.saveButton" v-if="createDialog.type==='edit'">保存</el-button>
          </el-form-item>
        </el-form>
        <el-dialog :visible.sync="imageDialog.visible" append-to-body>
          <img width="100%" :src="imageDialog.url" alt="">
        </el-dialog>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import {
    createLive, deleteLive,
    getLiveList,
    updateLive
  } from '../../api/live'
  import { getToken } from '../../utils/auth'
  import { url } from '../../utils/php'
const form = () => {
    return {
      title: '',
      teacher: '',
      start_time: '',
      end_time: '',
      thumbnail: '',
      live_imgae_url: '',
      teacher_introduction: '',
      live_video_url: '',
      sort: 10,
      live_type: 2,
      is_show: 2
    }
  }
  const rules = () => {
    return {
      title: [
        { required: true, message: '请输入直播标题', trigger: 'blur' }
      ],
      start_time: [
        { required: true, message: '请输入开始时间', trigger: 'blur' }
      ],
      // end_time: [
      //   { required: true, message: '请输入结束时间', trigger: 'blur' }
      // ],
      // thumbnail: [
      //   { required: true, message: '请填写缩略图地址', trigger: 'blur' }
      // ],
      // live_imgae_url: [
      //   { required: true, message: '请填写直播二维码图片地址', trigger: 'blur' }
      // ],
      teacher_introduction: [
        { required: true, message: '请输入主讲人介绍', trigger: 'blur' }
      ],
      // live_video_url: [
      //   { required: true, message: '请输入回放视频地址', trigger: 'blur' }
      // ],
      teacher: [
        { required: true, message: '请填写主讲人', trigger: 'blur' }
      ],
      sort: [
        { required: true, message: '请填写排序', trigger: 'blur' },
        { type: 'number', message: '排序必须为数字' }
      ],
      is_show: [
        { required: true, message: '请选择是否显示', trigger: 'blur' }
      ]
    }
  }
  export default {
    data() {
      return {
        table: {
          data: [],
          page: 1,
          total: 0,
          loading: false,
          limit: 10
        },
        imageDialog: {
          visible: false,
          url: ''
        },
        postForm: form(),
        createDialog: {
          action: url() + 'upload',
          thumbnailData: {},
          liveData: {},
          headers: {
            Authorization: 'bearer ' + getToken(),
            Accept: 'application/json'
          },
          liveImageList: [],
          thumbnailList: [],
          id: '',
          type: '',
          visible: false,
          title: '',
          provinces: [],
          academies: [],
          rules: rules(),
          loading: {
            academy: false,
            province: false
          },
          disabled: {
            saveButton: false
          }
        }
      }
    },
    mounted() {
      this.init()
    },
    methods: {
      handleImagePreview(file) {
        console.log(file)
        this.imageDialog.url = file.url
        this.imageDialog.visible = true
      },
      handleUploadSuccess(name) {
        return (response, file, fileList) => {
          console.log(response, file, fileList)
          this.postForm[name] = response.url
        }
      },
      handleImageRemove(name) {
        return (file, fileList) => {
          this.postForm[name] = ''
        }
      },
      handleClose() {
        this.resetFields()
      },
      handleEdit() {
        this.$refs['postForm'].validate((valid) => {
          if (valid) {
            this.createDialog.disabled = true
            updateLive(this.postForm, this.createDialog.id).then(res => {
              this.resetFields()
              this.createDialog.visible = false
              this.getList()
            }).finally(() => {
              this.createDialog.disabled = false
            })
          } else {
            return false
          }
        })
      },
      showEdit(row) {
        this.createDialog.id = row.id
        this.createDialog.type = 'edit'
        this.createDialog.title = '编辑直播'
        this.postForm.is_show = row.is_show
        this.postForm.live_imgae_url = row.live_imgae_url
        this.createDialog.liveImageList = [
          {
            url: row.live_imgae_url
          }
        ]
        this.postForm.live_video_url = row.live_video_url
        this.postForm.title = row.title
        this.postForm.start_time = row.start_time
        this.postForm.end_time = row.end_time
        this.postForm.teacher_introduction = row.teacher_introduction
        this.postForm.thumbnail = row.thumbnail
        this.postForm.live_type = row.live_type
        this.createDialog.thumbnailList = [
          {
            url: row.thumbnail
          }
        ]
        this.postForm.teacher = row.teacher
        this.createDialog.visible = true
        this.postForm.sort = row.sort
      },
      handleDelete(row) {
        this.$confirm('您正在进行删除操作, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteLive(row.id).then(() => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getList()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      handleCreate() {
        this.$refs['postForm'].validate((valid) => {
          if (valid) {
            this.createDialog.disabled = true
            createLive(this.postForm).then(res => {
              this.resetFields()
              this.createDialog.visible = false
              this.getList()
            }).finally(() => {
              this.createDialog.disabled = false
            })
          } else {
            return false
          }
        })
      },
      handleSizeChange(size) {
        this.table = () => {
          this.table.page = 1
          this.table.limit = size
          return this.table
        }
        this.getList()
      },
      handleCurrentChange(page) {
        this.table.page = page
        this.getList()
      },
      getList() {
        this.table.loading = true
        getLiveList({
          limit: this.table.limit,
          page: this.table.page
        }).then(res => {
          this.table.data = res.data.data
          this.table.total = res.data.meta.total
        }).finally(() => {
          this.table.loading = false
        })
      },
      showCreate() {
        this.createDialog.type = 'create'
        this.createDialog.title = '添加直播'
        this.createDialog.visible = true
      },
      resetFields() {
        this.postForm = form()
        this.createDialog.thumbnailList = []
        this.createDialog.liveImageList = []
      },
      init() {
        this.getList()
      }
    }
  }
</script>

<style scoped>

</style>
